package com.apes.fico.engine.model;

import com.apes.fico.masterdata.gl.model.AccountingSystem;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.ApesConst;
import com.apes.fico.masterdata.gl.model.AccountingModel;
import com.apes.fico.masterdata.gl.model.CashFlow;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

/**
 * @program: apestech
 * @description: 凭证模板
 * @author: Chengxm
 * @create: 2019-01-24 15:41
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VoucherTemplateEntry.CONST_TABLE_NAME,
        indexes = {
                @Index(columnList = "voucher_template_id,sequence",unique = true)
        }
)
public class VoucherTemplateEntry extends Model {
    public final static String CONST_TABLE_NAME = "fin_voucher_template_entry";
    @Id
    @Column(name = "id", length = 8)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "凭证模板分录")
    @Comment("编码")
    private String id;

    @NotNull
    @ManyToOne //(optional = false)
    @JoinColumn(name = "voucher_template_id", referencedColumnName = "id")
    @Comment("凭证模板")
    private VoucherTemplate voucherTemplate;

    @Transient
    private PartyRole company;

    public PartyRole getCompany() {
        return voucherTemplate.getCompany();
    }

    @Transient
    private AccountingSystem accountingSystem;

    public AccountingSystem getAccountingSystem() {
        return voucherTemplate.getAccountingSystem();
    }

    @Column(name = "sequence",nullable = false)
    @Comment("序号")
    private int sequence;

    @Column(name = "chart_code",nullable = false)
    @Comment("科目代码")
    private String charCode;

    @Column(name = "chart_name",nullable = false)
    @Comment("科目名称")
    private String chartName;

    @Column(name = "orient", length = 1)
    @Comment("方向")
    @States(name = "方向", values = {
            @State(id = AccountingModel.DEBIT, name = "借方"),
            @State(id = AccountingModel.CREDIT, name = "贷方")})
    private String orient;

    @Column(name = "summary_formula", length = 200)
    @Comment("摘要")
    private String summaryFormula;

    @Column(name = "amount_formula", length = 200)
    @Comment("发生额公式")
    private String amountFormula;

    @Column(name = "condition_formula", length = 200)
    @Comment("条件公式")
    private String conditionFormula;

    @Column(name = "aux_formula", length = 200)
    @Comment("核算公式")
    private String auxFormula;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "element_group_id", referencedColumnName = "id")
    @Comment("要素组")
    private ElementGroup elementGroup;

    @Column(name = "enable",nullable = false,columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("是否启用要素科目表")
    private boolean enable;

    @Column(name = "entry_group", length = 200)
    @Comment("分录组")
    private String entryGroup;

    @Column(name = "priority",nullable = false)
    @Comment("优先级")
    private int priority = 0;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "cash_flow_id", referencedColumnName = "id")
    @Comment("现金流量")
    private CashFlow cashFlow;

    @Column(name = "valid",nullable = false,columnDefinition = ApesConst.FIELD_BOOLEAN_TRUE)
    @Comment("有效标记")
    private boolean valid = true; //有效

    public String group() {
        if (StringUtils.isEmpty(entryGroup)) return id;
        return id + "-" + entryGroup;
    }

    @Transient
    private String businessName;

    public String getBusinessName() {
        return this.voucherTemplate !=null ? this.voucherTemplate.getBusinessType().getName():"";
    }
}
